package com.openbravo.pos.sql;

import com.openbravo.basic.BasicException;
import com.openbravo.data.loader.BaseSentence;
import com.openbravo.data.loader.DataField;
import com.openbravo.data.loader.DataResultSet;
import com.openbravo.pos.forms.JPanelView;
import com.openbravo.pos.panels.SQLTableModel;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.Date;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableCellRenderer.UIResource;
import javax.swing.table.TableCellRenderer;

public class JPanelSQLResult extends JPanel
{
  private static final Icon ICO_NULL = new ImageIcon(JPanelView.class.getResource("/com/openbravo/images/null.png"));
  private static final Icon ICO_TRUE = new ImageIcon(JPanelView.class.getResource("/com/openbravo/images/true.png"));
  private static final Icon ICO_FALSE = new ImageIcon(JPanelView.class.getResource("/com/openbravo/images/false.png"));
  private static final Icon ICO_BYTEA = new ImageIcon(JPanelView.class.getResource("/com/openbravo/images/bytea.png"));
  JButton jButton1;
  JButton jButton2;
  JButton jButton3;
  JButton jButton4;
  JPanel jPanel1;
  JPanel jPanel2;
  JPanel jPanel3;
  JScrollPane jScrollPane1;
  JScrollPane jScrollPane2;
  JSeparator jSeparator1;
  JToolBar jToolBar1;
  JTable m_jTableResult;
  JTextArea m_txtResulltText;

  public JPanelSQLResult()
  {
    initComponents();
    this.m_jTableResult.setDefaultRenderer(String.class, (TableCellRenderer) new StringRenderer());
    this.m_jTableResult.setDefaultRenderer(Double.class, (TableCellRenderer) new NumberRenderer());
    this.m_jTableResult.setDefaultRenderer(Integer.class, (TableCellRenderer) new NumberRenderer());
    this.m_jTableResult.setDefaultRenderer(Date.class, (TableCellRenderer) new DateRenderer());
    this.m_jTableResult.setDefaultRenderer(Boolean.class, (TableCellRenderer) new BooleanRenderer());
 //   this.m_jTableResult.setDefaultRenderer([B.class, (TableCellRenderer) new ByteaRenderer());
  }

  public boolean executeSentence(BaseSentence paramBaseSentence)
  {
    return executeSentence(paramBaseSentence, null);
  }

  public boolean executeSentence(BaseSentence paramBaseSentence, Object paramObject)
  {
    CardLayout localCardLayout = (CardLayout)(CardLayout)getLayout();
    try
    {
      DataResultSet localDataResultSet = paramBaseSentence.openExec(paramObject);
      if (localDataResultSet.updateCount() < 0)
      {
        localCardLayout.show(this, "resultset");
        DataField[] localObject = localDataResultSet.getDataField();
        SQLTableModel localSQLTableModel = new SQLTableModel(localObject);
        while (localDataResultSet.next())
          localSQLTableModel.addRow(localDataResultSet);
        localDataResultSet.close();
        paramBaseSentence.closeExec();
        this.m_jTableResult.setModel(localSQLTableModel);
      }
      else
      {
        localCardLayout.show(this, "updatecount");
        this.m_txtResulltText.setText("Update count: " + Integer.toString(localDataResultSet.updateCount()));
        this.m_txtResulltText.setCaretPosition(0);
      }
      return true;
    }
    catch (BasicException localBasicException)
    {
      localCardLayout.show(this, "updatecount");
      Object localObject = new StringWriter();
      localBasicException.printStackTrace(new PrintWriter((Writer)localObject));
      this.m_txtResulltText.setText(((StringWriter)localObject).toString());
      this.m_txtResulltText.setCaretPosition(0);
    }
    return false;
  }

  private void initComponents()
  {
    this.jPanel3 = new JPanel();
    this.jPanel1 = new JPanel();
    this.jToolBar1 = new JToolBar();
    this.jButton1 = new JButton();
    this.jButton2 = new JButton();
    this.jSeparator1 = new JSeparator();
    this.jButton3 = new JButton();
    this.jButton4 = new JButton();
    this.jScrollPane1 = new JScrollPane();
    this.m_jTableResult = new JTable();
    this.jPanel2 = new JPanel();
    this.jScrollPane2 = new JScrollPane();
    this.m_txtResulltText = new JTextArea();
    setLayout(new CardLayout());
    add(this.jPanel3, "card4");
    this.jPanel1.setLayout(new BorderLayout());
    this.jToolBar1.setFloatable(false);
    this.jButton1.setText("jButton1");
    this.jToolBar1.add(this.jButton1);
    this.jButton2.setText("jButton2");
    this.jToolBar1.add(this.jButton2);
    this.jSeparator1.setOrientation(1);
    this.jToolBar1.add(this.jSeparator1);
    this.jButton3.setText("jButton3");
    this.jToolBar1.add(this.jButton3);
    this.jButton4.setText("jButton4");
    this.jToolBar1.add(this.jButton4);
    this.jPanel1.add(this.jToolBar1, "North");
    this.jScrollPane1.setViewportView(this.m_jTableResult);
    this.jPanel1.add(this.jScrollPane1, "Center");
    add(this.jPanel1, "resultset");
    this.jPanel2.setLayout(new BorderLayout());
    this.m_txtResulltText.setEditable(false);
    this.jScrollPane2.setViewportView(this.m_txtResulltText);
    this.jPanel2.add(this.jScrollPane2, "Center");
    add(this.jPanel2, "updatecount");
  }

  static class BooleanRenderer extends DefaultTableCellRenderer.UIResource
  {
    public void setValue(Object paramObject)
    {
      if (paramObject == null)
        setIcon(JPanelSQLResult.ICO_NULL);
      else
        setIcon(((Boolean)paramObject).booleanValue() ? JPanelSQLResult.ICO_TRUE : JPanelSQLResult.ICO_FALSE);
    }
  }

  static class ByteaRenderer extends DefaultTableCellRenderer.UIResource
  {
    public void setValue(Object paramObject)
    {
      if (paramObject == null)
        setIcon(JPanelSQLResult.ICO_NULL);
      else
        setIcon(JPanelSQLResult.ICO_BYTEA);
    }
  }

  static class IconRenderer extends DefaultTableCellRenderer.UIResource
  {
    public IconRenderer()
    {
      setHorizontalAlignment(0);
    }

    public void setValue(Object paramObject)
    {
      setIcon((paramObject instanceof Icon) ? (Icon)paramObject : null);
    }
  }

  static class DateRenderer extends DefaultTableCellRenderer.UIResource
  {
    DateFormat formatter;

    public void setValue(Object paramObject)
    {
      if (this.formatter == null)
        this.formatter = DateFormat.getDateInstance();
      if (paramObject == null)
      {
        setIcon(JPanelSQLResult.ICO_NULL);
        setText(null);
      }
      else
      {
        setIcon(null);
        setText(this.formatter.format(paramObject));
      }
    }
  }

  static class NumberRenderer extends DefaultTableCellRenderer.UIResource
  {
    NumberFormat formatter = null;

    public NumberRenderer()
    {
      setHorizontalAlignment(4);
    }

    public void setValue(Object paramObject)
    {
      if (this.formatter == null)
        this.formatter = NumberFormat.getInstance();
      if (paramObject == null)
      {
        setIcon(JPanelSQLResult.ICO_NULL);
        setText(null);
      }
      else
      {
        setIcon(null);
        setText(this.formatter.format(paramObject));
      }
    }
  }

  static class StringRenderer extends DefaultTableCellRenderer.UIResource
  {
    public void setValue(Object paramObject)
    {
      if (paramObject == null)
      {
        setIcon(JPanelSQLResult.ICO_NULL);
        setText(null);
      }
      else
      {
        setIcon(null);
        setText(paramObject.toString());
      }
    }
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.pos.sql.JPanelSQLResult
 * JD-Core Version:    0.6.0
 */